/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            var _this =this;
            $.ajax({
                url: _global.url.api + 'club',
                type: 'get',
                async: false,
                success: function(res) {
                    if(res.code == 0) {
                        _this.set('info', res.data);
                    }
                }
            });
            this.fetch();
            this.event();
        },

        fetch: function() {
            var html=template('add-tpl',this.info);
            $("#clubinfo").html(html);
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'club_user',
                model: {
                    url:_global.url.api + 'club_user',
                    event: {
                        '.item-remove:click': 'remove'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },set: function($k, $v) {
            this[$k] = $v;
        },
        event: function() {
            this.data = {};
            var _this = this;
            $("#btn-clubuser").on("click",function(e){
                e.preventDefault();
                $("#main").show();
                $("#club").hide();
            });
            $("#btn-club").on("click",function(e){
                e.preventDefault();
                $("#main").hide();
                $("#club").show();
            });
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            //保存
            $('#btn_save').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.data.clubname = $("#clubname").val();
                if(_this.data.clubname==''){
                    Alert({msg:'请输入俱乐部名称',type:'error'});
                    return false;
                }
                _this.data.username = $('#username').val();
                if(_this.data.username==''){
                    Alert({msg:'请输入联系人',type:'error'});
                    return false;
                }
                _this.data.mobile = $("#mobile").val();
                if(_this.data.mobile==''){
                    Alert({msg:'请输入手机号码',type:'error'});
                    return false;
                }
                var reg =/^1[3|4|5|8|7][0-9]{9}$/;
                if(!reg.test(_this.data.mobile)){
                    Alert({msg:'手机号码格式有误',type:'error'});
                    return false;
                }
                _this.data.address = $("#address").val();
                _this.data.intro = $("#intro").val();
                if(new Model({
                        url: _global.url.api + 'club',
                        data: _this.data
                    }).put()) {
                }

            });

        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});